package com.ykt.common.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.kit.ResourceKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.ykt.controller.AdController;
import com.ykt.controller.AfterSalesController;
import com.ykt.controller.BaseDataController;
import com.ykt.controller.CameraController;
import com.ykt.controller.CaptchaController;
import com.ykt.controller.CategoryController;
import com.ykt.controller.ClientVersionController;
import com.ykt.controller.CommunityController;
import com.ykt.controller.FavoriteController;
import com.ykt.controller.GoodsCategoryController;
import com.ykt.controller.GoodsController;
import com.ykt.controller.MarketController;
import com.ykt.controller.OrderController;
import com.ykt.controller.RyController;
import com.ykt.controller.ShopCategoryController;
import com.ykt.controller.ShopController;
import com.ykt.controller.SysAddressController;
import com.ykt.controller.UploadController;
import com.ykt.controller.UserAddressController;
import com.ykt.controller.UserController;
import com.ykt.controller.UserLoggerController;
import com.ykt.controller.WeixinController;
import com.ykt.controller.WelcomeController;
import com.ykt.controller.Ys7Controller;
import com.ykt.interceptor.AccessInterceptor;
import com.ykt.interceptor.ExceptionInterceptor;
import com.ykt.model.Ad;
import com.ykt.model.Address;
import com.ykt.model.AgentFee;
import com.ykt.model.BaseData;
import com.ykt.model.Camera;
import com.ykt.model.CameraShop;
import com.ykt.model.Category;
import com.ykt.model.ClientVersion;
import com.ykt.model.Community;
import com.ykt.model.Goods;
import com.ykt.model.GoodsCategory;
import com.ykt.model.Market;
import com.ykt.model.MediaSource;
import com.ykt.model.Order;
import com.ykt.model.OrderItem;
import com.ykt.model.PayRecord;
import com.ykt.model.Service;
import com.ykt.model.Shop;
import com.ykt.model.ShopCategory;
import com.ykt.model.User;
import com.ykt.model.UserAddress;
import com.ykt.model.UserCollect;
import com.ykt.model.UserFeedback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class ApiConfig extends JFinalConfig {
    private final static Logger LOGGER = LoggerFactory.getLogger(ApiConfig.class);


    @Override
    public void configConstant(Constants me) {
        PropKit.use("config.properties"); // 加载配置文件

        me.setDevMode(true);
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/", WelcomeController.class);
        me.add("/user", UserController.class);
        me.add("/ys", Ys7Controller.class);
        me.add("/camera", CameraController.class);
        me.add("/upload", UploadController.class);
        me.add("/ry", RyController.class);
        me.add("/captcha", CaptchaController.class);
        me.add("/user/address", UserAddressController.class);// 用户地址服务
        me.add("/community", CommunityController.class);
        me.add("/order", OrderController.class);
        me.add("/favorite", FavoriteController.class);
        me.add("/market", MarketController.class);
        me.add("/goods", GoodsController.class);
        me.add("/shop", ShopController.class);
        me.add("/version", ClientVersionController.class);
        me.add("/sys/address", SysAddressController.class);// 系统地址服务
        me.add("/category", CategoryController.class);// 商铺分类服务
        me.add("/shopCategory", ShopCategoryController.class);// 商铺分类服务
        me.add("/goodsCategory", GoodsCategoryController.class);// 商铺分类服务
        me.add("/baseData", BaseDataController.class);// 基础数据服务
        me.add("/service", AfterSalesController.class);// 售后服务
        me.add("/weixin", WeixinController.class);// 微信支付服务
        me.add("/ad", AdController.class);// 广告服务
        me.add("/userLogger", UserLoggerController.class);// 用户日志服务
        

        LOGGER.debug("初始化configRoute");
    }

    @Override
    public void configPlugin(Plugins me) {
        LOGGER.debug("初始化configPlugin");
        // DB连接池插件
        Map<String, String> conf = ResourceKit.readProperties("jdbc.properties");
        C3p0Plugin c3p0Plugin = new C3p0Plugin(conf.get("jdbcUrl"), conf.get("user"), conf.get("password").trim());
        me.add(c3p0Plugin);

        // DB操作插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);

        arp.addMapping("user", User.class);
        arp.addMapping("camera", Camera.class);
        arp.addMapping("camera_shop", CameraShop.class);
        
        arp.addMapping("market", Market.class);
        arp.addMapping("community", Community.class);
        arp.addMapping("order", Order.class);
        arp.addMapping("order_item", OrderItem.class);
        arp.addMapping("user_address", UserAddress.class);
        arp.addMapping("user_feedback", UserFeedback.class);

        arp.addMapping("base_data", BaseData.class);
        arp.addMapping("category", Category.class);
        arp.addMapping("shop_category", ShopCategory.class);
        arp.addMapping("goods_category", GoodsCategory.class);
        arp.addMapping("pay_record", PayRecord.class);
        arp.addMapping("goods", Goods.class);
        arp.addMapping("user_collect", UserCollect.class);
        arp.addMapping("shop", Shop.class);
        arp.addMapping("address", Address.class);
        arp.addMapping("client_version", ClientVersion.class);
        arp.addMapping("service", Service.class);
        arp.addMapping("media_source", MediaSource.class);
        arp.addMapping("ad", Ad.class);
        arp.addMapping("agent_fee", AgentFee.class);

        arp.setDevMode(true);
        arp.setShowSql(true);
        me.add(arp);

        // 缓存插件
        me.add(new EhCachePlugin());
    }

    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new AccessInterceptor());
        me.add(new ExceptionInterceptor());

    }

    @Override
    public void configHandler(Handlers me) {
        // TODO Auto-generated method stub

    }

}
